package com.hilaver.dzmis.web.servlet.product;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiAccessoryOthers;
import com.hilaver.dzmis.product.ProductAccessoryOthers;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductOrderItem;
import com.hilaver.dzmis.service.impl.ProductAccessoryOthersServiceImpl;

/**
 * Servlet implementation class for Servlet: OrderServlet
 * 
 */
public class ProductAccessoryOthersServlet extends
		com.hilaver.dzmis.web.servlet.AbstractBaseServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;
	public final static int ACTION_GET_ALL_ACCESSORY_OTHERS = 6;
	private ProductAccessoryOthersServiceImpl poService;
	private ProductAccessoryOthers accessoryOthers;
	public ProductAccessoryOthersServlet() {
		super();
		this.simpleName = ProductAccessoryOthers.class.getSimpleName();
		this.fullName = ProductAccessoryOthers.class.getName();
		this.poService = new ProductAccessoryOthersServiceImpl();
		this.poService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			writer.write(execute(request));
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);
		this.poService.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));
		super.setAcUser(request, poService);
		String rtn = "";

		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";

			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			rtn = this.poService.edit(this.accessoryOthers);
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			rtn = this.poService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			rtn = this.poService.get(this.id);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"

			break;
		case ACTION_GET_ALL:
			this.setId(request);
			rtn = this.poService.getAll(this.id);
			break;
			
		case ACTION_GET_ALL_ACCESSORY_OTHERS:
			this.setId(request);
			rtn = this.poService.getAllAccessoryOthers(this.id);
			break;
		}

		return rtn;
	}

	protected void fillObj(HttpServletRequest request) throws Exception {
		super.fillObj(request);
		this.accessoryOthers = (ProductAccessoryOthers)this.obj;
		
		BiAccessoryOthers biAccessoryOthers = new BiAccessoryOthers();
		biAccessoryOthers.setId(Integer.parseInt(request.getParameter("aoId")));
		ProductIdentification pi = new ProductIdentification();
		pi.setId(Integer.parseInt(request.getParameter("piId")));
		
		this.accessoryOthers.setBiAccessoryOthers(biAccessoryOthers);
		this.accessoryOthers.setProductIdentification(pi);
	}	
}